package main

import (
	"fmt"
	"github.com/gogf/gf/container/garray"
	"sort"
)

type Data struct {
	Id        int `json:"id"`
	FirstSort int `json:"first_sort"`
	SecSort   int `json:"sec_sort"`
}

type List1 []Data

func main() {
	//data := List1{{1, 1, 3}, {2, 2, 9999999}, {3, 3, 9999999}, {4, 4, 9999999}, {5, 5, 2}, {6, 6, 4}, {7, 7, 9999999}, {8, 8, 9999999}} //2,5,1,6,3,4,7,8
	//data := List1{{1, 1, 8}, {2, 2, 9999999}, {3, 3, 9999999}, {4, 4, 9999999}, {5, 5, 2}, {6, 6, 1000}, {7, 7, 9999999}, {8, 8, 9999999}} //2,5,3,4,7,8,1,6
	//data := List1{{1, 1, 8}, {2, 2, 2}, {3, 3, 1}, {4, 4, 3}, {5, 5, 4}, {6, 6, 5}, {7, 7, 7}, {8, 8, 6}} //3,2,4,5,6,8,7,1
	//data := List1{{1, 1, 8}, {2, 2, 2}, {3, 3, 1}, {4, 4, 3}, {5, 5, 1000}, {6, 6, 5}, {7, 7, 7}, {8, 8, 6}} //3,2,4,6,8,7,1,5
	//data := List1{{1, 1, 3}, {2, 2, 9999999}, {3, 3, 9999999}, {4, 4, 1}, {5, 5, 2}, {6, 6, 4}, {7, 7, 9999999}, {8, 8, 9999999}} //4,5,1,6,2,3,7,8
	data := List1{{1, 1, 9999999}, {2, 2, 9999999}, {3, 3, 9999999}, {4, 4, 7}, {5, 5, 9999999}, {6, 6, 9999999}, {7, 7, 9999999}, {8, 8, 9999999}} //1,2,3,5,6,7,4,8

	var (
		a = garray.NewArray(false)
		b []Data
	)

	for _, v := range data {
		if v.SecSort == 9999999 {
			a.Append(v)
		} else {
			b = append(b, v)
		}
	}

	sort.SliceStable(b, func(i, j int) bool {
		return b[i].SecSort < b[j].SecSort
	})

	for _, v := range b {
		if v.SecSort == 1 {
			if a.Len() == 0 {
				a.Append(v)
			} else {
				a.PushLeft(v)
			}
			continue
		}

		if v.SecSort > a.Len() {
			a.Append(v)
			continue
		}

		a.InsertAfter(v.SecSort-2, v)

	}

	//res := a.Slice()
	//var res1 []Data
	//for _, v := range res {
	//	if item, ok := v.(Data); ok {
	//		res1 = append(res1, item)
	//	}
	//}
	//fmt.Printf("res1 = %+v\nres1类型:%T\n", res1, res1)
	//fmt.Printf("res = %+v\nres类型:%T\n", res, res)

	fmt.Println(a.Slice())
}
